RgMolecule -> Molecule ClassCastException

User 870ab5b546

02-04-2012 18:10:39

The code:


    public static String toString(Molecule mol, String format) {
try {
return MolExporter.exportToFormat(mol, format);
} catch (IOException e) { ; }
return null;
} // toString(Molecule, String)

The exception report:


java.lang.ClassCastException: chemaxon.struc.Molecule cannot be cast to chemaxon.struc.RgMolecule
at chemaxon.marvin.io.formats.smiles.SmilesExport.toSMILES(SmilesExport.java:926)
at chemaxon.marvin.io.formats.smiles.SmilesExport.convert(SmilesExport.java:762)
at chemaxon.formats.MolExporter.exportToObject(MolExporter.java:1298)
at chemaxon.formats.MolExporter.exportToObject(MolExporter.java:1242)
at chemaxon.formats.MolExporter.exportToFormat(MolExporter.java:1138)
at com.epoch.chem.ChemUtils.toString(ChemUtils.java:207)

I don't understand why SmilesExport.toSMILES() is trying to cast a Molecule to an RgMolecule.  I'm definitely passing a Molecule to MolExporter.exportToFormat().  Can you advise?  I'm using JChem 5.9.0.

ChemAxon 5433b8e56b

04-04-2012 11:14:26

Hi Bob,


I have moved this topic to the structure representation and file formats related forum, my collagues will answer soon.


Thank you for you patience.


Regards,
Istvan

ChemAxon d26931946c

04-04-2012 13:12:40

Hi Bob, 


I can't reproduce the exception.


This code works fine both with Molecule and RgMolecule. Can you give us some hint? (the structure that gives the exception or the way you call this code) 


Molecule m = new Molecule();
m.add(new MolAtom(6));
System.err.println(MolExporter.exportToFormat(m, "smiles"));

Regards,


Peter

User 870ab5b546

04-04-2012 14:26:06

Here's a more thorough description of the path through the code:


    public static void checkValence(Molecule mol) throws ValenceException {
...
final String PENTACOORD_N = "[$(*=,#[NX3]=,#*),$(*:n(=,#*):*),$(*=,#N#*)]";
try {
final Molecule pentacoordN = MolImporter.importMol(PENTACOORD_N); // line 1283
if (MolFunctions.containsSubstruct(mol, pentacoordN)) {
throw new ValenceException(VALENCE_ERROR);
} // if molecule contains pentavalent N
} catch (MolFormatException e) { // unlikely
; // do nothing
} catch (MolFileException e) { // unlikely
; // do nothing
} // try
} // checkValence(Molecule)

public static boolean containsSubstruct(Molecule respMol,
Molecule substruct, int chgRadIso) throws MolFileException {
...
try {
match = search.isMatching();
debugPrint(SELF + "response ", respMol, (match // line 885
? " contains" : " does not contain"),
" the substructure, ", substruct);
} catch (SearchException e2) {
Utils.alwaysPrint("Error in " + SELF);
e2.printStackTrace();
throw new MolFileException(ERROR + SELF + e2.getMessage());
}
} // containsSubstruct(Molecule, Molecule, int)

private static void printObject(Object obj, String format) {
...
} else if (obj instanceof Molecule) {
try {
System.out.print(ChemUtils.toString((Molecule) obj, format)); // line 130; format = "SMILES"
} catch (IllegalArgumentException e1) {
try {
System.out.print(ChemUtils.toString((Molecule) obj, SMARTS));
} catch (IllegalArgumentException e2) {
System.out.print(ChemUtils.toString((Molecule) obj, MRV));
} // try
} // try
} else if (obj instanceof MDocument) {
...
} // printObject(Object, String)

public static String toString(Molecule mol, String format) {
try {
return MolExporter.exportToFormat(mol, format);
} catch (IOException e) { ; }
return null;
} // toString(Molecule, String)

The exception seems to occur when I try to export [$(*=,#[NX3]=,#*),$(*:n(=,#*):*),$(*=,#N#*)] into SMILES format.


I changed printObject() to catch an Exception instead of an IllegalArgumentException.  I no longer get the ClassCastException, and the log now says:


MolFunctions.containsSubstruct: response [OH2+]C1CCCCO1 does not contain the substructure, null

So JChem is still unable to export [$(*=,#[NX3]=,#*),$(*:n(=,#*):*),$(*=,#N#*)], but at least it now just returns a null value instead of throwing an exception.  

ChemAxon d26931946c

05-04-2012 11:31:02

Hi Bob, 


[$(*=,#[NX3]=,#*),$(*:n(=,#*):*),$(*=,#N#*)] contains features that can't be written out to SMILES format. 


However I can export this without any problem to SMARTS: 


 public void testMoleculeSmilesExport() throws Exception{
Molecule m= MolImporter.importMol("[$(*=,#[NX3]=,#*),$(*:n(=,#*):*),$(*=,#N#*)]");
System.err.println(MolExporter.exportToFormat(m, "smarts"));
}

I'm not familiar with our search implementation, I will ask a colleague about that.


Regards, 


Peter

User 870ab5b546

05-04-2012 13:31:29

I guess the major issue is that the exception gives no relevant information as to the cause of the error.  And I used to get an IllegalArgumentException in JChem 5.6, whereas now, in JChem 5.9, I am getting a ClassCastException. And the export to SMARTS is failing.  Even when I catch the ClassCastException in printObject() and try to convert the molecule to SMARTS, MolExporter.exportToFormat() is throwing an IOException, which I catch, hence the null value being returned.


Could the problem be caused by the cast to an Object followed by a cast back to the Molecule?


    public static void checkValence(Molecule mol) throws ValenceException {
...
final String PENTACOORD_N = "[$(*=,#[NX3]=,#*),$(*:n(=,#*):*),$(*=,#N#*)]";
try {
final Molecule pentacoordN = MolImporter.importMol(PENTACOORD_N); // line 1283
if (MolFunctions.containsSubstruct(mol, pentacoordN)) {
throw new ValenceException(VALENCE_ERROR);
} // if molecule contains pentavalent N
...
} // checkValence(Molecule)

public static boolean containsSubstruct(Molecule respMol,
Molecule substruct, int chgRadIso) throws MolFileException {
...
match = search.isMatching();
debugPrint(SELF + "response ", respMol, (match
? " contains" : " does not contain"),
" the substructure, ", substruct);
...
} // containsSubstruct(Molecule, Molecule, int)

private static void debugPrint(Object... msg) { // casts to Object

Utils.printToLog(msg);
}

public static void printToLog(Object[] msg) {
printToLog(msg, SMILES);
} // alwaysPrint(Object[], String)

public static void printToLog(Object[] msg, String format) {
for (Object obj : msg) printObject(obj, format);
System.out.println();
} // printToLog(Object[], String)

private static void printObject(Object obj, String format) {
...
} else if (obj instanceof Molecule) {
try {
System.out.print(ChemUtils.toString((Molecule) obj, format)); // casts back to Molecule
} catch (Exception e1) {
try {
System.out.print(ChemUtils.toString((Molecule) obj, SMARTS));
} catch (Exception e2) {
System.out.print(ChemUtils.toString((Molecule) obj, MRV));
} // try
} // try
} else ...
} // printObject(Object, String)

public static String toString(Molecule mol, String format) {
try {
return MolExporter.exportToFormat(mol, format); // is throwing IOException
} catch (IOException e) { ; }
return null;
} // toString(Molecule, String)

ChemAxon efa1591b5a

17-04-2012 13:56:22

Hi Bob,


Apologies for the long silence. The problem you experienced and reported appears to be a cross-team issue, this is why we are a bit sluggish in addressing it. Thank you for your patience.


BR
Miklos 

ChemAxon d9cc14700b

18-04-2012 14:03:34

Hi Bob,


Could you please send us the molecule which is passed to your 'checkValence' method when the ClassCastException occurs? My suspition is that it is an RgMolecule which might cause this issue.


Thanks and Regards,


Gabor

User 870ab5b546

11-05-2012 13:31:59

I am experiencing the problem again. This time, the offending structure is [H][#6-]([H])=,:[#6+](-[#6])[O-].  


INFO: ChemUtils.toString: ClassCastException caught while converting molecule to smarts
MechFlowsValid.isResponseMatching: unknown exception: chemaxon.struc.Molecule cannot be cast to chemaxon.struc.RgMolecule
java.lang.ClassCastException: chemaxon.struc.Molecule cannot be cast to chemaxon.struc.RgMolecule
at chemaxon.marvin.io.formats.smiles.SmilesExport.toSMILES(SmilesExport.java:926)
at chemaxon.marvin.io.formats.smiles.SmilesExport.convert(SmilesExport.java:762)
at chemaxon.formats.MolExporter.exportToObject(MolExporter.java:1298)
at chemaxon.formats.MolExporter.exportToObject(MolExporter.java:1242)
at chemaxon.formats.MolExporter.exportToFormat(MolExporter.java:1138)
at com.epoch.chem.ChemUtils.toString(ChemUtils.java:209)
at com.epoch.mechanisms.MechSolver.lookForInvalidProducts(MechSolver.java:651)

In our installation that is still using JChem 5.6.0.0, this line does NOT throw an error:


System.out.print(((Molecule) obj).toFormat(SMARTS));

However, when I try to use the same line in JChem 5.9.0:


        try {
return MolExporter.exportToFormat(mol, format);
} catch (ClassCastException e) {
Utils.alwaysPrint("ChemUtils.toString: ClassCastException "
+ "caught while converting molecule to ", format,
"; using deprecated Molecule.toFormat() method instead.");
return mol.toFormat(SMARTS);
} catch (IOException e) { ; }

I still get an exception:


INFO: ChemUtils.toString: ClassCastException caught while converting molecule to smarts; using deprecated Molecule.toFormat() method instead.
MechFlowsValid.isResponseMatching: unknown exception: chemaxon.struc.Molecule cannot be cast to chemaxon.struc.RgMolecule
java.lang.ClassCastException: chemaxon.struc.Molecule cannot be cast to chemaxon.struc.RgMolecule
at chemaxon.marvin.io.formats.smiles.SmilesExport.toSMILES(SmilesExport.java:926)
at chemaxon.marvin.io.formats.smiles.SmilesExport.convert(SmilesExport.java:762)
at chemaxon.formats.MolExporter.exportToObject(MolExporter.java:1298)
at chemaxon.formats.MolExporter.exportToObject(MolExporter.java:1242)
at chemaxon.formats.MolExporter.exportToFormat(MolExporter.java:1138)
at chemaxon.formats.StructureExporterUtil.exportToFormat(StructureExporterUtil.java:62)
at chemaxon.struc.Molecule.exportToFormat(Molecule.java:1063)
at chemaxon.struc.Molecule.toFormat(Molecule.java:1035)
at com.epoch.chem.ChemUtils.toString(ChemUtils.java:214)
at com.epoch.mechanisms.MechSolver.lookForInvalidProducts(MechSolver.java:651)

Please fix this serious problem.

User 870ab5b546

11-05-2012 13:52:04










ghornyak wrote:

Hi Bob,


Could you please send us the molecule which is passed to your 'checkValence' method when the ClassCastException occurs? My suspition is that it is an RgMolecule which might cause this issue.


Thanks and Regards,


Gabor



Looks like our messages crossed.  The molecule O=N(=O)C1=CC=CC=C1 is triggering the error: note the "null".


MolFunctions.containsSubstruct: response O=N(=O)C1=CC=CC=C1 contains the substructure, null

The code:


    public static void checkValence(Molecule mol) throws ValenceException {
...
final String PENTACOORD_N = "[$(*=,#[NX3]=,#*),$(*:n(=,#*):*),$(*=,#N#*)]";
try {
final Molecule pentacoordN = MolImporter.importMol(PENTACOORD_N);
if (MolFunctions.containsSubstruct(mol, pentacoordN)) {
throw new ValenceException(VALENCE_ERROR);
} // if molecule contains pentavalent N
} catch (MolFormatException e) { // unlikely
; // do nothing
} catch (MolFileException e) { // unlikely
; // do nothing
} // try
} // checkValence(Molecule)


public static boolean containsSubstruct(Molecule respMol,
Molecule substruct) throws MolFileException {
return containsSubstruct(respMol, substruct, EXACT_CHG_RAD_ISO);
} // containsSubstruct(Molecule, Molecule)

public static boolean containsSubstruct(Molecule respMol,
Molecule substruct, int chgRadIso) throws MolFileException {
final String SELF = "MolFunctions.containsSubstruct: ";
final MolSearch search = new MolSearch();
...
search.setTarget(respMol);
search.setQuery(substruct);
try {
match = search.isMatching();
debugPrint(SELF + "response ", respMol, match ? " contains"
: " does not contain", " the substructure, ", substruct);
} catch (SearchException e2) {
Utils.alwaysPrint("Error in " + SELF);
e2.printStackTrace();
throw new MolFileException(ERROR + SELF + e2.getMessage());
}
return match;
} // containsSubstruct(Molecule, Molecule, int)

private static void debugPrint(Object... msg) {
Utils.printToLog(msg);
}

public static void alwaysPrint(Object... msg) {
printToLog(msg, SMILES);
} // alwaysPrint(Object...)

private static void printObject(Object obj, String format) {
if (obj == null) {
System.out.print("null");
...
} else if (obj instanceof Molecule) {
try {
System.out.print(ChemUtils.toString((Molecule) obj, format));
} catch (Exception e1) {
try {
System.out.print(ChemUtils.toString((Molecule) obj, SMARTS));
} catch (Exception e2) {
System.out.print(ChemUtils.toString((Molecule) obj, MRV));
} // try
} // try
...
} // printObject(Object, String)

public static String toString(Molecule mol, String format) {
try {
return MolExporter.exportToFormat(mol, format);
} catch (ClassCastException e) {
Utils.alwaysPrint("ChemUtils.toString: ClassCastException "
+ "caught while converting molecule to ", format,
"; using deprecated Molecule.toFormat() method instead.");
return mol.toFormat(SMARTS);
} catch (IOException e) { ; }
return null;
} // toString(Molecule, String)

[H][#6-]([H])=,:[#6+](-[#6])[O-] also triggers the error, but it's not going through the checkValence() method before it gets to ChemUtils.toString().  (The only way I was able to extract the [H][#6-]([H])=,:[#6+](-[#6])[O-] as the SMARTS of the molecule was by generating it through JChem 5.6.)  

ChemAxon d9cc14700b

15-05-2012 11:17:27

Hi Bob,


I could not reproduce the issue with none of the mentioned molecules. I noticed however that the stack trace you included is not in sync with the code snippets: I can't find method com.epoch.mechanisms.MechSolver.lookForInvalidProducts. Could you please post it as well?


Additionally could you please make a separate test with only the MolExporter, something like this?


    public static void main(String[] args) throws Exception {

        Molecule molecule = MolImporter.importMol("[H][#6-]([H])=,:[#6+](-[#6])[O-]");

        try {
            System.out.println(MolExporter.exportToFormat(molecule, "SMILES"));
        } catch (IOException e) {
            System.out.println("OK, exception thrown because molecule can not be exported to SMILES");
        }
        System.out.println(MolExporter.exportToFormat(molecule, "SMARTS"));
    }

 



This is how I tried to reproduce it and failed, let's see if it works for you (or perhaps I am missing something).


 


Best Regards,


Gabor

User 870ab5b546

15-05-2012 13:53:10

lookForInvalidProducts() merely calls ChemUtils.toString():


    private void lookForInvalidProducts(boolean hasOddElectronBond,
boolean tooManyElectrons) throws MechError {
final String SELF = "MechSolver.lookForInvalidProducts: ";
if (hasOddElectronBond) {
makePretty(molecule);
final String smarts = ChemUtils.toString(molecule, SMARTS);
Utils.alwaysPrint(SELF + "obtained product ", smarts,
" with one or more half-integral bonds; "
+ "throwing MechError.");
throw new MechError("half-integral bond",
ODD_ELECTRON_BOND, smarts);
...

private void makePretty(Molecule mol) {
MolFunctions.pentavalentNToYlideNoClone(mol);
StereoFunctions.allCrissCrossToWavy(mol);
} // makePretty(Molecule)

public static void pentavalentNToYlideNoClone(Molecule mol) {
final String SELF = "MolFunctions.pentavalentNToYlideNoClone: ";
debugPrintMRV(SELF + "starting as \n", mol);
try {
AppConfig.pentavalentNStdizer.standardize(mol);
} catch (SearchException e) {
Utils.alwaysPrint(SELF + "SearchException while standardizing");
e.printStackTrace();
}
debugPrintMRV(SELF + "mol converted to \n", mol);
} // pentavalentNToYlideNoClone(Molecule)

public static void allCrissCrossToWavy(Molecule molecule) {
allCrissCrossToWavy(new Molecule[] {molecule});
} // allCrissCrossToWavy(Molecule)

public static void allCrissCrossToWavy(Molecule[] mols) {
final String ERROR = "ChemUtils.allCrissCrossToWavy: ";
if (mols != null) try {
final Standardizer stdizer =
new Standardizer("convertdoublebonds:wiggly");
for (Molecule mol : mols) {
if (mol != null) stdizer.standardize(mol);
} // for each molecule
} catch (StandardizerException e) {
System.out.println(ERROR + "StandardizerException.");
e.printStackTrace();
} catch (SearchException e) {
System.out.println(ERROR + "SearchException.");
e.printStackTrace();
}
} // allCrissCrossToWavy(Molecule[])

AppConfig.pentavalentNStdizer is initialized with this XML:


<?xml version="1.0" encoding="UTF-8"?>
<!-- Standardizer configuration file -->

<StandardizerConfiguration Version ="0.1">
<Actions>
<Transformation ID="N_ylide_double"
Structure="[#34,#16,#8,#7,#6;0:2]=[#7;0:1]=[#34,#16,#8,#7,#6;0:2]>>[#34,#16,#8,#7,#6;0:2]=[#7;+:1]-[#34,#16,#8,#7,#6;-:2]"/>
<Transformation ID="N_ylide_triple"
Structure="[#34,#16,#8,#7,#6;0:2]=[#7;0:1]#[#34,#16,#8,#7,#6;0:2]>>[#34,#16,#8,#7,#6;0:2]=[#7;+:1]=[#34,#16,#8,#7,#6;-:2]"/>
</Actions>
</StandardizerConfiguration>

User 870ab5b546

15-05-2012 19:57:21










ghornyak wrote:

Additionally could you please make a separate test with only the MolExporter, something like this?



Here is the result:


bob@epoch-virtual:epoch$ java importExportTest
OK, exception thrown because molecule can not be exported to SMILES
[H][#6-]([H])=,:[#6+](-[#6])[O-]

Path and classpath defined as:


PATH=.:$PATH:$HOME/bin:/home/aceorg/aceorg/jchem5.9.0/bin:/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin

CLASSPATH=.:/home/aceorg/aceorg/DEV-3.5/ace/build:/usr/share/java:/home/aceorg/aceorg/jchem5.9.0/lib/jchem.jar:/home/aceorg/aceorg/otherJar/ojdbc5.jar

The entire contents of importExportTest.java:


import chemaxon.struc.Molecule;
import chemaxon.formats.MolExporter; // new to Marvin 5.9
import chemaxon.formats.MolImporter;
import java.io.IOException;

public class importExportTest {

public static void main(String[] args) throws Exception {
Molecule molecule = MolImporter.importMol("[H][#6-]([H])=,:[#6+](-[#6])[O-]");
try {
System.out.println(MolExporter.exportToFormat(molecule, "SMILES"));
} catch (IOException e) {
System.out.println("OK, exception thrown because molecule can not be exported to SMILES");
}
System.out.println(MolExporter.exportToFormat(molecule, "SMARTS"));
}
}

User 870ab5b546

15-05-2012 20:01:32

I think that the reason you are not seeing the same problems that I am seeing is that I am manipulating the molecule, changing its bonds, etc., before trying to print it.  


Please send me your email address and I will send you our code so that you can try to reproduce the problem.

User 870ab5b546

15-05-2012 23:37:02

I think I may have isolated the error in at least one case.  When [H][#6-]([H])=,:[#6+](-[#6])[O-] is subject to


AppConfig.pentavalentNStdizer.standardize(mol);

then the ClassCastException occurs the next time I try to export it to SMARTS format.  This Standardizer is initiated with this XML:


<?xml version="1.0" encoding="UTF-8"?>
<!-- Standardizer configuration file -->

<StandardizerConfiguration Version ="0.1">
<Actions>
<Transformation ID="N_ylide_double"
Structure="[#34,#16,#8,#7,#6;0:2]=[#7;0:1]=[#34,#16,#8,#7,#6;0:2]>>[#34,#16,#8,#7,#6;0:2]=[#7;+:1]-[#34,#16,#8,#7,#6;-:2]"/>
<Transformation ID="N_ylide_triple"
Structure="[#34,#16,#8,#7,#6;0:2]=[#7;0:1]#[#34,#16,#8,#7,#6;0:2]>>[#34,#16,#8,#7,#6;0:2]=[#7;+:1]=[#34,#16,#8,#7,#6;-:2]"/>
</Actions>
</StandardizerConfiguration>

I comment out the line that applies the standardization, and there is no problem.

ChemAxon d9cc14700b

16-05-2012 07:57:36

Still no success. Please send me your code to ghornyak _at_ chemaxon.com.


Thanks and Regards,


Gabor